//
//  HealthAllView.m
//  Ginix
//
//  Created by fzq on 2025/2/23.
//

#import "HealthAllView.h"
#import "HealthReportsCell.h"
#import "HealthComInfoVC.h"
#import "HealthListModel.h"

@interface HealthAllView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * listView;
@property (nonatomic,strong) NSMutableArray * listArray;
@property (nonatomic,assign) NSInteger currentPage;

@end
@implementation HealthAllView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if(self = [super initWithFrame:frame]){
        [self setSubViewLayout];
        self.currentPage = 1;
        [self requestData];
        [self addRefresh];
    }
    return self;
}

-(void)setSubViewLayout{
    
    [self addSubview:self.listView];
    
}

-(void)addRefresh{
        
   @WeakObj(self)
    [self.listView headerRefreshBlock:^{
        @StrongObj(self)
        self.currentPage = 1;
        [self requestData];
    }];
    
    [self.listView footerRefreshBlock:^{
        @StrongObj(self)
        self.currentPage ++;
        [self requestData];
    }];
}
-(void)requestData{
    @WeakObj(self)
    NSDictionary * params = @{@"pageNub":@(self.currentPage),@"pageSize": @(10)};
    [SPNetworkingIns POST:PORT(@"bh-center/api/invite/getUserHealthTest") params:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        [self.listView.mj_footer endRefreshing];
        [self.listView.mj_header endRefreshing];
        NSArray * dataArr = [[responseObject objectForKey:@"data"] objectForKey:@"list"];
        NSMutableArray * array = [NSMutableArray array];
        if (dataArr.count) {
            for (NSDictionary * item in dataArr) {
                HealthListModel * model = [[HealthListModel alloc]init];
                model = [HealthListModel yy_modelWithDictionary:item];
                [array addObject:model];
            }
        }
        if (self.currentPage == 1) {
            [self.listArray removeAllObjects];
        }
        if (array.count > 0) {
            [self.listArray addObjectsFromArray:array];
        }else {
            self.currentPage --;
        }
        [self.listView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [self.listView.mj_footer endRefreshing];
        [self.listView.mj_header endRefreshing];
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        
    return  70 * FMScaleX;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HealthReportsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HealthReportsCell"];
    HealthListModel * model = self.listArray[indexPath.row];
    if (model) {
        [cell setInfoModel:model type:1];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HealthListModel * model = self.listArray[indexPath.row];
    if (model) {
        HealthComInfoVC * vc = [[HealthComInfoVC alloc]init];
        vc.ID = model.ID;
        [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    }
}

-(UITableView *)listView{
    
    if (!_listView) {
        _listView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, self.getHeight)];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        _listView.backgroundColor = UIColor.clearColor;
        _listView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_listView registerClass:[HealthReportsCell class] forCellReuseIdentifier:@"HealthReportsCell"];
    }
    return _listView;
}

-(NSMutableArray *)listArray{
    
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}

@end
